package CH9;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
 * @author: LiuBoTao
 * @date: 2022/10/13
 * @ApiNote
 */
public class GuiDemo3 {
    public static void main(String[] args) {
        MyFrame frame=new MyFrame();
        frame.init("Liubotao");

        //MyFrame frame2=new MyFrame();
        //frame2.init("Liubotao22");
    }
}

class MyFrame extends JFrame{
    GridBagLayout gridBag;
    GridBagConstraints gbc;


    void init(String strTitle) {
        setTitle(strTitle);
        //this.setBounds(100,90,500,400);
        setBounds(100,90,500,400);
        this.setResizable(false);//设置窗口是否可调整大小
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置窗体行为：用户单击关闭按钮时，窗体关闭了

        this.gridBag= new GridBagLayout();    // 布局管理器
        this.setLayout(gridBag);

        gbc=new GridBagConstraints();//布局管理其约束，格式约束
        gbc.fill = GridBagConstraints.HORIZONTAL;//让组件在横向上填充整个网格，默认是取组件默认大小

        gbc.insets=new Insets(10,5,10,5);

        JLabel labelUser=new JLabel("用户名：");
        this.add(labelUser);

        JTextField textUser=new JTextField("aaaaaa");
        textUser.setText("奥斯卡冷冻机房看拉时代峻峰辣椒水两地分居拉伸了");
        this.add(textUser);

        JLabel labelPWD=new JLabel("密码：");
        this.add(labelPWD);

        JPasswordField textPwd=new JPasswordField("bbb");
        textPwd.setEchoChar('*');
        System.out.println(textPwd.getText());
        System.out.println(textPwd.getPassword());
        this.add(textPwd);

        JLabel labelSexy=new JLabel("性别：");
        this.add(labelSexy);

        JComboBox comboBoxSexy=new JComboBox();
        comboBoxSexy.addItem("男");//0--->1
        comboBoxSexy.addItem("女");//1--->2
        comboBoxSexy.insertItemAt("保密",0);//0
        comboBoxSexy.setSelectedItem("女");/**/
        comboBoxSexy.setSelectedIndex(0);/**/
        this.add(comboBoxSexy);
        MyItemListener myItemListener=new MyItemListener();
        comboBoxSexy.addItemListener(myItemListener);

        JLabel labelRole=new JLabel("身份：");
        this.add(labelRole);

        JRadioButton rBtnT=new JRadioButton("老师");
        this.add(rBtnT);

        JRadioButton rBtnS=new JRadioButton("学生");
        this.add(rBtnS);

        JLabel labelInterest=new JLabel("兴趣：");
        this.add(labelInterest);
        JCheckBox cbSing = new JCheckBox("唱歌");
        this.add(cbSing);
        JCheckBox cbDance = new JCheckBox("跳舞");
        this.add(cbDance);
        JCheckBox  cbSwing= new JCheckBox("游泳");
        this.add(cbSwing);

        String str=new String("确定");
        JButton  buttonOK=new JButton(str);
        this.add(buttonOK);
        //System.out.println("buttonOK="+buttonOK);

        String str2=new String("取消");
        JButton  buttonCancel=new JButton(str2);
        this.add(buttonCancel);

        MyActionListenerOK  listenerOK=new MyActionListenerOK();
        listenerOK.getTxt(textUser,rBtnT,rBtnS);
        listenerOK.getPwdTxt(textPwd,comboBoxSexy,buttonCancel,buttonOK);
        buttonOK.addActionListener(listenerOK);
        buttonCancel.addActionListener(listenerOK);


        addComponent(labelUser,3,2,1);
        addComponent(textUser,3,3,3);
        addComponent(labelPWD,4,2,1);
        addComponent(textPwd,4,3,3);
        addComponent(labelSexy,5,2,1);
        addComponent(comboBoxSexy,5,3,3);
        addComponent(labelRole,6,2,1);
        addComponent(rBtnT,6,3,1);
        addComponent(rBtnS,6,4,1);
        addComponent(labelInterest,7,2,1);
        addComponent(cbSing,7,3,1);
        addComponent(cbDance,7,4,1);
        addComponent(cbSwing,7,5,1);
        addComponent(buttonOK,8,3,1);
        addComponent(buttonCancel,8,4,1);

        //  f.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);//设置窗体行为：用户单击关闭按钮时，窗体啥都不干
        this.setVisible(true);//设置窗口为可见，默认不可见/**/
    }


    void addComponent(Component component, //组件，按钮，下拉框，文本，label
                      int rowIndex,int colIndex,int cellNum)
    {
        this.gbc.gridy=rowIndex;//行8
        gbc.gridx=colIndex;//列4
        this.gbc.gridwidth=cellNum;//占1列
        this.gridBag.addLayoutComponent(component, gbc); // 内部使用的仅是 gbc 的副本
    }
}

class  MyItemListener implements ItemListener{
    @Override
    public void itemStateChanged(ItemEvent e) {

    }
}

class MyActionListenerOK implements ActionListener{
    JTextField textUser;
    JPasswordField textPwd;
    JComboBox cmBox;
    JRadioButton rBtnT;
    JRadioButton rBtnS;
    JButton  buttonCancel;
    JButton  buttonOK;
    void getTxt(JTextField textUser,JRadioButton rBtnT,JRadioButton rBtnS){
        this.textUser=textUser;
        this.rBtnT=rBtnT;
        this.rBtnS=rBtnS;
    }
    void getPwdTxt(JPasswordField textPwd,JComboBox cmBox,JButton  buttonCancel,JButton  buttonOK){
        this.textPwd=textPwd;
        this.cmBox=cmBox;
        this.buttonCancel=buttonCancel;
        this.buttonOK=buttonOK;
    }
    @Override
    public void actionPerformed(ActionEvent eeee) {
        //System.out.println("aaaa");
        //JOptionPane.showMessageDialog(
        //        null,
        //        "message",
        //        "title",
        //        //JOptionPane.ERROR_MESSAGE);
        //        JOptionPane.WARNING_MESSAGE);
        System.out.println(textUser.getText());
        //System.out.println(textPwd.getText());
        System.out.println(textPwd.getPassword());
        System.out.println(cmBox.getSelectedItem());
        System.out.println("老师："+rBtnT.isSelected());
        System.out.println("学生："+rBtnS.isSelected());

    /*    //System.out.println("eeee.getSource()="+eeee.getSource());
        if(eeee.getSource()==buttonOK){
            System.out.println("用户点击了【确定】按钮");
        }
        else if(eeee.getSource()==buttonCancel) {
            System.out.println("用户点击了【取消】按钮");
        }*/
        //System.out.println("eeee.getActionCommand()="+eeee.getActionCommand());
        //if(eeee.getActionCommand()=="确定"){//比较的是地址，不是内容
        if(eeee.getActionCommand().equals("确定")){//比较的才是内容
            System.out.println("用户点击了【确定】按钮");
        //} else if(eeee.getActionCommand()=="取消") {
        } else if(eeee.getActionCommand().equals("取消")) {
            System.out.println("用户点击了【取消】按钮");
        }
    }
}